package com.itbaizhan.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.ruiji.common.CustomException;
import com.itbaizhan.ruiji.common.Result;
import com.itbaizhan.ruiji.dto.SetmealDto;
import com.itbaizhan.ruiji.pojo.Category;
import com.itbaizhan.ruiji.pojo.Dish;
import com.itbaizhan.ruiji.pojo.Setmeal;
import com.itbaizhan.ruiji.pojo.SetmealDish;
import com.itbaizhan.ruiji.service.CategorySevice;
import com.itbaizhan.ruiji.service.SetmealDishService;
import com.itbaizhan.ruiji.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetMealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategorySevice categoryService;
    @PostMapping
    public Result<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.addMealwithDish(setmealDto);
        return Result.success("添加成功");
    }

    @GetMapping("/page")
    public Result<Page> page(int page, int pageSize, String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        Page<Setmeal> pageinfo = new Page(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealService.page(pageinfo,queryWrapper);
        BeanUtils.copyProperties(pageinfo,dtoPage,"records");
        List<Setmeal> records = pageinfo.getRecords();
        List<SetmealDto> list = records.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return Result.success(dtoPage);
    }

    @GetMapping("/list")
    public Result<List<SetmealDto>> list(Setmeal  setmeal){
        log.info("查询菜品：{}",setmeal);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        queryWrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> list = setmealService.list(queryWrapper);
        List<SetmealDto> setmealDtoList=list.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long itemId = item.getId();
            LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SetmealDish::getSetmealId,itemId);
            lambdaQueryWrapper.orderByAsc(SetmealDish::getUpdateTime);
            List<SetmealDish> setmealDishList = setmealDishService.list(lambdaQueryWrapper);
            setmealDto.setSetmealDishes(setmealDishList);
            return setmealDto;
        }).collect(Collectors.toList());
        return Result.success(setmealDtoList);
    }


    @DeleteMapping
    @Transactional
    public Result<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return Result.success("删除成功");
    }
}
